package cn.iocoder.yudao.module.handguard.service.referrerrecord;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.handguard.controller.app.referrerrecord.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.referrerrecord.ReferrerRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.handguard.convert.referrerrecord.ReferrerRecordConvert;
import cn.iocoder.yudao.module.handguard.dal.mysql.referrerrecord.ReferrerRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.handguard.enums.ErrorCodeConstants.*;

/**
 * 推荐人记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ReferrerRecordServiceImpl implements ReferrerRecordService {

    @Resource
    private ReferrerRecordMapper referrerRecordMapper;

    @Override
    public Long createReferrerRecord(ReferrerRecordCreateReqVO createReqVO) {
        // 插入
        ReferrerRecordDO referrerRecord = ReferrerRecordConvert.INSTANCE.convert(createReqVO);
        referrerRecordMapper.insert(referrerRecord);
        // 返回
        return referrerRecord.getId();
    }

    @Override
    public void updateReferrerRecord(ReferrerRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateReferrerRecordExists(updateReqVO.getId());
        // 更新
        ReferrerRecordDO updateObj = ReferrerRecordConvert.INSTANCE.convert(updateReqVO);
        referrerRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteReferrerRecord(Long id) {
        // 校验存在
        validateReferrerRecordExists(id);
        // 删除
        referrerRecordMapper.deleteById(id);
    }

    private void validateReferrerRecordExists(Long id) {
        if (referrerRecordMapper.selectById(id) == null) {
            throw exception(REFERRER_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ReferrerRecordDO getReferrerRecord(Long id) {
        return referrerRecordMapper.selectById(id);
    }

    @Override
    public List<ReferrerRecordDO> getReferrerRecordList(Collection<Long> ids) {
        return referrerRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ReferrerRecordDO> getReferrerRecordPage(ReferrerRecordPageReqVO pageReqVO) {
        return referrerRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ReferrerRecordDO> getReferrerRecordList(ReferrerRecordExportReqVO exportReqVO) {
        return referrerRecordMapper.selectList(exportReqVO);
    }

}
